package com.ilxqx.framework.config;

import com.ilxqx.framework.system.converter.bind.EntityEnumConverterFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.Formatter;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 配置web
 * @author venus
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    /// 配置静态资源映射
    /**
     * Add handlers to serve static resources such as images, js, and, css
     * files from specific locations under app application root, the classpath,
     * and others.
     *
     * @param registry 注册对象
     */
    /*@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
    }*/

    /**
     * 配置跨域请求
     * @param registry 跨域注册对象
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 注册所有的请求都需要跨域
        registry
            .addMapping("/sys/**")
            .allowedMethods("*")
            .allowedOrigins("*")
            .exposedHeaders("X-Auth-Token", "X-Access-Key")
            .maxAge(3600);

        registry
                .addMapping("/log/**")
                .allowedMethods("*")
                .allowedOrigins("*")
                .exposedHeaders("X-Auth-Token", "X-Access-Key")
                .maxAge(3600);

        registry
                .addMapping("/auth/**")
                .allowedMethods("*")
                .allowedOrigins("*")
                .exposedHeaders("X-Auth-Token", "X-Access-Key")
                .maxAge(3600);

        registry
                .addMapping("/app/**")
                .allowedMethods("*")
                .allowedOrigins("*")
                .exposedHeaders("X-Auth-Token", "X-Access-Key")
                .maxAge(3600);
    }

    /**
     * Add {@link Converter Converters} and {@link Formatter Formatters} in addition to the ones
     * registered by default.
     *
     * @param registry 注册器
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverterFactory(new EntityEnumConverterFactory());
    }

}
